package com.yungam.common.bean.report;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;

@Data
@Accessors(chain = true)
@TableName("report_data")
public class ReportRow {

    public ReportRow() {}
    public ReportRow(String productSn, String processNo) {
        this.productSn = productSn;
        this.processNo = processNo;
    }

    private static final ObjectMapper objectMapper = new ObjectMapper();
    @TableId(type = IdType.AUTO)
    private Integer id;                         // id
    private Integer reportType;                 // 报告类型
    private String productSn;                   // 产品编号
    private String processNo;                   // 工序编号
    @JsonIgnore
    private byte[] rowData;                     // 行数据
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date CreateTime;                    // 创建时间
    private Boolean isDeleted;                  // 是否删除
    @TableField(exist = false)
    private Map data;

    public void setData(Map data) {
        this.data = data;
        try {
            this.rowData = objectMapper.writeValueAsString(data).getBytes(StandardCharsets.UTF_8);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    public void setRowData(byte[] rowData) {
        this.rowData = rowData;
        try {
            this.data = objectMapper.readValue(new String(rowData, StandardCharsets.UTF_8), Map.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

}

